<template>
	<view>
		<custom-navigation-bar title="申请详情"></custom-navigation-bar>
		<view class="main">
			<!-- 申请基本信息 -->
			<view class="detail-item">
				<view class="detail-label">申请ID:</view>
				<view class="detail-content">{{ applicationDetail.applicationId }}</view>
			</view>
			<view class="detail-item">
				<view class="detail-label">申请时间:</view>
				<view class="detail-content">{{ applicationDetail.applicationTime }}</view>
			</view>
			<view class="detail-item">
				<view class="detail-label">姓名:</view>
				<view class="detail-content">{{ applicationDetail.name }}</view>
			</view>
			<view class="detail-item">
				<view class="detail-label">性别:</view>
				<view class="detail-content">{{ sex }}</view>
			</view>
			<view class="detail-item">
				<view class="detail-label">电话:</view>
				<view class="detail-content">{{ applicationDetail.phone }}</view>
			</view>
			<view class="detail-item">
				<view class="detail-label">申请区域:</view>
				<view class="detail-content">{{ applicationDetail.region }}</view>
			</view>
			<view class="detail-item">
				<view class="detail-label">申请类型:</view>
				<view class="detail-content">{{ type }}</view>
			</view>
			<view class="detail-item" v-if="!applicationDetail.type">
				<view class="detail-label">身份证号:</view>
				<view class="detail-content">{{ applicationDetail.idCard }}</view>
			</view>
			<view class="detail-item" v-if="applicationDetail.type">
				<view class="detail-label">法人姓名:</view>
				<view class="detail-content">{{ applicationDetail.legalPersonName }}</view>
			</view>
			<view class="detail-item" v-if="applicationDetail.type">
				<view class="detail-label">法人身份证号:</view>
				<view class="detail-content">{{ applicationDetail.legalPersonIdCard }}</view>
			</view>
			<view class="detail-item" v-if="images.length > 0">
				<view class="detail-label">身份证照片:</view>
				<view class="detail-content">
					<view v-for="(img, index) in images" :key="index" class="image-wrapper">
						<image :src="img" class="detail-image"></image>
					</view>
				</view>
			</view>
			<view class="detail-item" v-if="applicationDetail.type">
				<view class="detail-label">企业名称:</view>
				<view class="detail-content">{{ applicationDetail.companyName }}</view>
			</view>
			<view class="detail-item" v-if="applicationDetail.type">
				<view class="detail-label">营业执照编码:</view>
				<view class="detail-content">{{ applicationDetail.license }}</view>
			</view>


			<view class="detail-item" v-if="applicationDetail.type">
				<view class="detail-label">营业执照照片:</view>
				<view class="detail-content">
					<image :src="license" class="detail-image"></image>
				</view>
			</view>
			<view class="detail-item">
				<view class="detail-label">开户行:</view>
				<view class="detail-content">{{ applicationDetail.bankName }}</view>
			</view>
			<view class="detail-item">
				<view class="detail-label">银行卡号:</view>
				<view class="detail-content">{{ applicationDetail.bankCard }}</view>
			</view>
			<view class="detail-item">
				<view class="detail-label">附加说明:</view>
				<view class="detail-content">
					<input type="text" class="form-item-input" placeholder="请输入附加说明" @input="updateInstructions" />
				</view>
			</view>
			<view class="detail-item">
				<view style="width: 84%;float: left;margin-left: 8%;">
					<view class="btn" @click="success()">审核通过</view>
				</view>
			</view>
			<view class="detail-item">
				<view style="width: 84%;float: left;margin-left: 8%;">
					<view class="btn" @click="failed()">审核不通过</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		defineComponent
	} from 'vue';
	import http from '../../utils/http';
	const applicationDetail = ref({});
	const type = ref("")
	const images = ref([])
	const license = ref("")
	const sex = ref("")

	// 处理路径，去掉不需要的前缀
	const processedIdImages = computed(() => {
		return applicationDetail.value.idImage.map(img => img.replace('/pages/approve', ''));
	});

	function getImage64(path) {
		http.request({
			url: '/api/utils/get_image64',
			method: 'POST',
			data: {
				"path": path
			},
		}).then((responseData) => {
			return responseData.data
		}).catch((err) => {
			// 在这里处理请求失败的情况
			console.log("err", err)
		});
	}
	// 在页面加载时接收参数
	onLoad(async (query) => {
		if (query.application) {
			applicationDetail.value = JSON.parse(decodeURIComponent(query.application));
			type.value = applicationDetail.value.type == 1 ? "企业" : "个人"
			sex.value = applicationDetail.value.sex == 1 ? "女" : "男"
			console.log("type", type.value)
			console.log("a", applicationDetail.value)
			for (const path of applicationDetail.value.idImage) {
				try {
					// 调用后端 API 获取 Base64 编码
					http.request({
						url: '/api/utils/get_image64',
						method: 'POST',
						data: {
							"path": path
						},
					}).then((responseData) => {
						images.value.push(responseData.data)
					}).catch((err) => {
						// 在这里处理请求失败的情况
						console.log("err", err)
					});
				} catch (error) {
					console.error("Error fetching image:", error);
				}
			}
			http.request({
				url: '/api/utils/get_image64',
				method: 'POST',
				data: {
					"path": applicationDetail.value.licenseImage
				},
			}).then((responseData) => {
				license.value = responseData.data
			}).catch((err) => {
				// 在这里处理请求失败的情况
				console.log("err", err)
			});
		} else {
			uni.showToast({
				icon: 'error',
				title: "出错了"
			})
		}
	})
	function updateInstructions(e){
		applicationDetail.value.applicationAdditionalInstructions = e.target.value
	}
	function success(){
		applicationDetail.value.applicationStatus = 1
		applicationDetail.value.applicationAdditionalInstructions = "审核通过"
		applicationDetail.value.approverUserId = getApp().globalData.user.userId
		applicationDetail.value.approverName = getApp().globalData.user.name
		http.request({
			url:"/api/application/update_application_by_cityPartner",
			method:"POST",
			data:{
				"application":applicationDetail.value
			}
		}).then((responseData)=>{
			console.log("审核成功")
			
			http.request({
				url:"/api/cityPartner/insert_identity",
				method:"POST",
				data:{
					"application":applicationDetail.value
				}
			}).then((consponseData)=>{
				console.log("插入成功")
				uni.showToast({
					icon: "success",
					title: "审核成功",
					duration: 1500,
					complete: function() {
						setTimeout(function() {
							uni.navigateBack({
								delta: 1 // 返回到上一页
							});
						}, 1500); // 延迟 1.5 秒，和 showToast 的 duration 相同
					}
				})
			})
			
		}).catch((err)=>{
			console.log("审核失败")
		})
	}
	function failed(){
		applicationDetail.value.applicationStatus = 2
		applicationDetail.value.approverUserId = getApp().globalData.user.userId
		applicationDetail.value.approverName = getApp().globalData.user.name
		http.request({
			url:"/api/application/update_application_by_cityPartner",
			method:"POST",
			data:{
				"application":applicationDetail.value
			}
		}).then((responseData)=>{
			console.log("审核成功")
			
			uni.showToast({
				icon: "success",
				title: "审核成功",
				duration: 1500,
				complete: function() {
					setTimeout(function() {
						uni.navigateBack({
							delta: 1 // 返回到上一页
						});
					}, 1500); // 延迟 1.5 秒，和 showToast 的 duration 相同
				}
			})
		}).catch((err)=>{
			console.log("审核失败")
		})
	}
</script>

<style scoped>
	.main {
		padding: 20rpx;
	}

	.detail-item {
		margin-bottom: 20rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
	}

	.detail-label {
		font-weight: bold;
		width: 25%;
	}

	.detail-content {
		width: 75%;
		word-wrap: break-word;
	}

	.detail-image {
		width: 100rpx;
		height: 100rpx;
		margin-right: 10rpx;
	}
	.form-item{
			 width: 100%;float: left;background: #FFFFFF;height: auto;
	}
	.btn{
			 width: 100%;height: 80rpx;float: left;background:#2AE8DC;border-radius: 20rpx;text-align: center;line-height: 80rpx;color: white;
	}
</style>